package com.showde.shoppingmall.business.logic;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;

import com.kim.net.NetEntity;
import com.showde.shoppingmall.business.NetCallbackImplBase.Notify;
import com.showde.shoppingmall.business.ProductFetchOnCriteriaBusiness;
import com.showde.shoppingmall.db.ProductAHRDBCommand;
import com.showde.shoppingmall.domain.ProductAHR;
import com.showde.shoppingmall.netentity.ProductSearchEntity.FETCH_TYPE;

public class ProductAHRFetchOnCriteriaLogic {

	private static final String TAG = "ProductFetchOnCriteriaLogic";

	/**
	 * 获取广告、热门、人气类商品
	 * 
	 * @param context
	 * @param fetchType
	 * @param uiCallbackInterface
	 */
	public static final void fetch(final Context context,
			final FETCH_TYPE fetchType,
			final UICallbackInterface uiCallbackInterface) {
		ProductFetchOnCriteriaBusiness fetchOnCriteriaBusiness = new ProductFetchOnCriteriaBusiness(
				new Notify() {

					@Override
					public void notifyNetCallback(NetEntity netEntity) {
						processResult(context, uiCallbackInterface, netEntity,
								fetchType);
					}

					@Override
					public void notifyError(int httpResponseCode,
							String errorMsg) {
						processError(errorMsg, uiCallbackInterface);
					}
				}, fetchType);
		fetchOnCriteriaBusiness.doBusiness();
	}

	/**
	 * 处理错误
	 */
	private static final void processError(String errorMsg,
			UICallbackInterface uiCallbackInterface) {
		Log.d(TAG, "errorMsg:" + errorMsg);
		uiCallbackInterface.onError(errorMsg);
	}

	/**
	 * 获取数据成功，处理结果
	 * 
	 * @param context
	 * @param uiCallbackInterface
	 * @param netEntity
	 */
	private static final void processResult(final Context context,
			final UICallbackInterface uiCallbackInterface, NetEntity netEntity,
			FETCH_TYPE fetchType) {
		JSONObject jsonObject = netEntity.result;
		try {
			if (!"0".equals(jsonObject.getInt("ReturnCode") + "")) {
				uiCallbackInterface.onError("fail::" + netEntity.receiveData);
				return;
			}

			JSONArray jsonArray = jsonObject.getJSONArray("List");
			if (jsonArray != null && jsonArray.length() > 0) {
				ProductAHR product = null;
				List<ProductAHR> products = new ArrayList<ProductAHR>();
				for (int i = 0; i < jsonArray.length(); i++) {
					JSONObject arrayItem = jsonArray.getJSONObject(i);
					product = new ProductAHR();
					product.setProductID(arrayItem.getInt("ID") + "");
					product.setProductName(arrayItem.getString("NAME"));
					product.setProductTitle(arrayItem.getString("TITLE"));
					product.setProductLogo(arrayItem.getString("LOGO"));
					product.setProductCategoryID(arrayItem.getInt("TYPEID")
							+ "");
					product.setProductPrice((float) arrayItem
							.getDouble("PRICE"));
					switch (fetchType) {
					case SEARCH_TYPE_HOT:// 热门商品
						product.setIsHot(1);
						break;
					case SEARCH_TYPE_ADVERTISEMENT:// 广告商品
						product.setIsAdvertisement(1);
						break;
					}
					products.add(product);
				}
				ProductAHRDBCommand productDBCommand = new ProductAHRDBCommand(
						context);
				productDBCommand.add(products);
				productDBCommand = null;
				CallbackData data = new CallbackData();
				data.data = products;
				uiCallbackInterface.onSuccess(data);
				return ;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		uiCallbackInterface.onError("receiveData:" + netEntity.receiveData);
	}
}
